'''
Created on 28/03/2010

@author: miguel
'''
import pygame
from pygame.sprite import Sprite
from vec2d import vec2d
from Instruction import Instruction
from GameBoard import GameBoard

class Bug(Sprite):
    (ALIVE, EXPLODING, DEAD) = range(3)
    (UP, DOWN, LEFT, RIGHT) = range(4)
    BUG_IMAGE_STOP = './data/bug_stop.png'
    BUG_IMAGE_WALKING1 = './data/bug_walking1.png'
    BUG_IMAGE_WALKING2 = './data/bug_walking2.png'
    #BUG_IMAGE_EXPLODED = './data/bug_exploded.png'
    BUG_IMAGE_EXPLODED1 = './data/bug_explosion_big1.png'
    BUG_IMAGE_EXPLODED2 = './data/bug_explosion_big2.png'

    EXPLOSION_FILE = './data/explosion.wav'

    def __init__(self, screen, game, field, init_position, instructions_list, owner):
        """initial constructor
        
        Keyword arguments:
        screen -- the screen where the bug will be drawn
        game -- the main object of the game which contains everything needed
        field -- square where the bug will be drawn
        init_position -- initial position where the bug will be drawn
        instructions_list -- list of object instruction which contains the instructions the bug will follow
        owner -- owner of the object; the player or the computer
        
        """
        
        Sprite.__init__(self)

        self.status = self.ALIVE
        self.screen = screen
        self.game = game
        self.field = field
        self.pos = vec2d(init_position)
        #self.initial_image = pygame.image.load(self.BUG_IMAGE)
        self.exploding_image1 = pygame.image.load(self.BUG_IMAGE_EXPLODED1)
        self.exploding_image2 = pygame.image.load(self.BUG_IMAGE_EXPLODED2)
        self.explosion = False
        self.image_bug_stop = pygame.image.load(self.BUG_IMAGE_STOP)
        self.image_bug_walking1 = pygame.image.load(self.BUG_IMAGE_WALKING1)
        self.image_bug_walking2 = pygame.image.load(self.BUG_IMAGE_WALKING2)
        self.counter = 0
        self.walking_movement = 1
        self.image = self.image_bug_stop
        self.rect = self.image.get_rect()
        self.direction = self.UP
        self.instructions_list = instructions_list
        self.movement_counter = 0
        self.owner = owner
        self.old_status_old_position = self.game.game_board.get_info_position(self.pos)
        self.old_status_position = self.game.game_board.get_info_position(self.pos)
        self.game.game_board.take_position(self.pos, self.owner)

        self.explosion_sound = pygame.mixer.Sound(self.EXPLOSION_FILE)

    def update(self, time_passed):
        if len(self.instructions_list) > 0:
            instruction = self.instructions_list[0]

            if instruction.get_instruction() == Instruction.MOVE_UP:
                # change image walking
                if self.counter > 3:
                    if self.walking_movement == 1:
                        self.image = self.image_bug_walking1
                        self.walking_movement = 2
                    elif self.walking_movement == 2:
                        self.image = self.image_bug_walking2
                        self.walking_movement = 1
                    self.counter = 0
                self.counter = self.counter + 1
                # rest
                self.sum_pos = vec2d(0, -30)
                self.sum_movement = vec2d(0, -1)
                if self.direction != self.UP:
                    self.image = self.image_bug_stop
                self.direction = self.UP
            elif instruction.get_instruction() == Instruction.MOVE_DOWN:
                # change image walking
                if self.counter > 3:
                    if self.walking_movement == 1:
                        self.image = pygame.transform.rotate(self.image_bug_walking1, 180)
                        self.walking_movement = 2
                    elif self.walking_movement == 2:
                        self.image = pygame.transform.rotate(self.image_bug_walking2, 180)
                        self.walking_movement = 1
                    self.counter = 0
                self.counter = self.counter + 1
                #rest
                self.sum_pos = vec2d(0, +30)
                self.sum_movement = vec2d(0, +1)
                if self.direction != self.DOWN:
                    self.image = pygame.transform.rotate(self.image_bug_stop, 180)
                self.direction = self.DOWN
            elif instruction.get_instruction() == Instruction.MOVE_LEFT:
                #change image walking
                if self.counter > 3:
                    if self.walking_movement == 1:
                        self.image = pygame.transform.rotate(self.image_bug_walking1, 90)
                        self.walking_movement = 2
                    elif self.walking_movement == 2:
                        self.image = pygame.transform.rotate(self.image_bug_walking2, 90)
                        self.walking_movement = 1
                    self.counter = 0
                self.counter = self.counter + 1
                #rest
                self.sum_pos = vec2d(-30, 0)
                self.sum_movement = vec2d(-1, 0)
                if self.direction != self.LEFT:
                    self.image = pygame.transform.rotate(self.image_bug_stop, 90)
                self.direction = self.LEFT
            elif instruction.get_instruction() == Instruction.MOVE_RIGHT:
                #change image walking
                if self.counter > 3:
                    if self.walking_movement == 1:
                        self.image = pygame.transform.rotate(self.image_bug_walking1, 270)
                        self.walking_movement = 2
                    elif self.walking_movement == 2:
                        self.image = pygame.transform.rotate(self.image_bug_walking2, 270)
                        self.walking_movement = 1
                    self.counter = 0
                self.counter = self.counter + 1
                #rest
                self.sum_pos = vec2d(30, 0)
                self.sum_movement = vec2d(1, 0)
                if self.direction != self.RIGHT:
                    self.image = pygame.transform.rotate(self.image_bug_stop, 270)
                self.direction = self.RIGHT
            elif instruction.get_instruction() == Instruction.STOP:
                #self.image = self.image_bug_stop
                pass
            elif instruction.get_instruction() == Instruction.EXPLODE:
                if self.explosion == False:
                    self.game.explosion(self.pos)
                    self.explosion = True
                if self.status != self.EXPLODING:
                    self.explosion_sound.play()
                    self.position_to_release = self.pos
                    self.pos = self.pos + vec2d(-30, -30)
                self.status = self.EXPLODING
                # change image walking
                if self.counter > 1:
                    if self.walking_movement == 1:
                        self.image = self.exploding_image1
                        self.walking_movement = 2
                    elif self.walking_movement == 2:
                        self.image = self.exploding_image2
                        self.walking_movement = 1
                    self.counter = 0
                self.counter = self.counter + 1
                # rest
                #self.image = self.exploding_image


            if instruction.get_instruction() != Instruction.STOP and instruction.get_instruction() != Instruction.EXPLODE:
                if self.movement_counter == 0:
                    self.old_pos = self.pos
                    self.new_pos = self.pos + self.sum_pos
                    if self.game.game_board.is_position_available(self.new_pos, self.owner):
                        self.old_status_position = self.game.game_board.get_info_position(self.new_pos)
                        self.movement = True
                        self.game.game_board.take_position(self.new_pos, self.game.game_board.USER_BUG)
                    else:
                        self.movement = False
                if self.movement == True:
                    self.pos = self.pos + self.sum_movement
                    self.movement_counter = self.movement_counter + 1
                else:
                    self.movement_counter = self.movement_counter + 1
                if self.movement_counter >= 30:
                    if self.movement == True:
                        self.game.game_board.restore_position(self.old_pos, self.old_status_old_position)
                        self.old_status_old_position = self.old_status_position
                    self.movement_counter = 0

            instruction.set_time(instruction.get_time() - 1)
            if instruction.get_time() == 0:
                self.instructions_list.pop(0)
            else:
                self.instructions_list[0] = instruction
        else:
            self.status = self.DEAD

    def draw(self):
        if self.status == self.ALIVE or self.status == self.EXPLODING:
            self.rect = self.image.get_rect().move(self.pos)
            self.screen.blit(self.image, self.rect)
        elif self.status == self.DEAD:
            self.game.game_board.restore_position(self.position_to_release, self.old_status_position)
            if self.owner == GameBoard.USER_BUG:
                self.game.player.bugs_number = self.game.player.bugs_number - 1
            elif self.owner == GameBoard.COM_BUG:
                self.game.computer.bugs_number = self.game.computer.bugs_number - 1
            self.kill()

    def return_myself_if_dead(self):
        if self.status == self.DEAD:
            return self

    def hit_bug(self, position_list):
        if self.pos == position_list[0] or self.pos == position_list[1] or self.pos == position_list[2] or self.pos == position_list[3] or self.pos == position_list[4]:
            self.status = self.DEAD
            self.position_to_release = self.pos
